package cn.dengta.webapp.ai.entity;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Nullable;

import cn.dengta.common.util.Common;
import cn.dengta.context.model.WesKit;
import cn.dengta.context.web.RequestContextImpl;
import cn.dengta.webapp.ai.model.AiStatus;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.user.entity.User;
import cn.dengta.webapp.user.vo.TagVO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * AI 信息表
 *
 * @author lizhichao
 * @since 2024-04-22
 */
@Setter
@Getter
@TableName("dt_ai")
public class Ai extends TimeBasedEntity {

	/** 官方AI用户ID */
	public static final Long OFFICIAL_AI_USER_ID = 0L;

	/** AI创作者用户ID，当为官方AI时，ID固定为0 */
	Long userId;
	/** 头像 */
	String avatar;
	/** 私聊背景 */
	String backdrop;
	/** 昵称 */
	String nickname;
	/** 性别：1=男；0=女 */
	Integer gender;
	/** 生日 */
	Date birthday;
	/** 身份 */
	Long identity;
	/** 人物关系 */
	Long relation;
	/** 对你的称呼 */
	String callYou;
	/** 故事 */
	@Nullable
	String story;
	/** 开场白 */
	@Nullable
	String openRemarks;
	/** 音色配置 */
	String timbre;
	/** 对话示例 */
	String sampleMsg;
	/** 在线状态：0=设置下线；1=设置上线 */
	Integer online;
	/** 审核状态：-99=平台冻结下线；-1=审核不通过；0=待审核；1=审核通过 */
	Integer status;
	/** 操作时间 */
	Date operateTime;
	/** 聊天次数 */
	Integer chatNum;
	/** 权重，默认为1，值越大显示越靠前 */
	Integer weight;
	/** 所属分包 */
	String wesKits;

	/** 排序分数值 */
	private transient int score;

	public void init(User user, Date now) {
		initTime(now);
		online = 0;
		status = AiStatus.WAIT_VERIFY.getValue();
		chatNum = 0;
		weight = 1;
		gender = user.getGender().value;
		userId = user.getId();
	}

	public String findWesKitLabel(){
		if (StringUtil.notEmpty(wesKits)) {
			List<Integer> split = Common.split(wesKits, Common.CHAR_SEP, Integer::parseInt, true);
			return split.stream().map(WesKit::of).map(WesKit::getLabel).collect(Collectors.joining(","));
		}
		return null;
	}
	/**
	 * 是否是官方AI
	 */
	public boolean isOfficial() {
		return OFFICIAL_AI_USER_ID.equals(userId);
	}

	public boolean isPass() {
		return isPass(status);
	}

	public boolean isFreeze() {
		return isFreeze(status);
	}

	public static boolean isFreeze(Integer status) {
		return AiStatus.FREEZE.eq(status);
	}

	public static boolean isPass(Integer status) {
		return AiStatus.PASS.eq(status);
	}


	public static List<TagVO> frontTags(List<TagVO> tags) {
		return X.map(tags, key -> key.stream().peek(t -> t.i18nName(RequestContextImpl.get().sessionLanguage()))
				.sorted(Comparator.comparingLong(TagVO::getId)).collect(Collectors.toList()));
	}


	public static final String USER_ID = "user_id";
	public static final String AVATAR = "avatar";
	public static final String NICKNAME = "nickname";
	public static final String GENDER = "gender";
	public static final String BIRTHDAY = "birthday";
	public static final String IDENTITY = "identity";
	public static final String RELATION = "relation";
	public static final String CALL_YOU = "call_you";
	public static final String STORY = "story";
	public static final String OPEN_REMARKS = "open_remarks";
	public static final String TIMBRE = "timbre";
	public static final String SAMPLE_MSG = "sample_msg";
	public static final String ONLINE = "online";
	public static final String STATUS = "status";
	public static final String CHAT_NUM = "chat_num";
	public static final String WEIGHT = "weight";
	public static final String WES_KIT = "wes_kit";
	public static final String OPERATE_TIME = "operate_time";

}
